<template>
	
	<view >
		<view class="search-box">
		  <!-- 使用 uni-ui 提供的搜索组件 -->
		  <uni-search-bar @input="input" :radius="100" cancelButton="none" focus></uni-search-bar>
		</view>
		<!-- 搜索栏目结束  -->
		<!-- 搜索建议列表 -->
		<view v-if="searchList.length !=0" class="sugg-list" >
		  <view class="sugg-item" v-for="(item, i) in searchList" :key="i" @click="gotoDetail(item.goods_id)">
		    <view class="goods-name">{{item.goods_name}}</view>
		    <uni-icons type="arrowright" size="16"></uni-icons>
		  </view>
		</view>
		<!-- 搜索历史列表 -->

		
		<view v-else>
			<!-- 搜索历史 -->
			<view class="history-box">
			  <!-- 标题区域 -->
			  <view class="history-title">
			    <text>搜索历史</text>
			    <uni-icons type="trash" size="17" @click="clearlist"></uni-icons>
			  </view>
			  <!-- 列表区域 -->
			  <view class="history-list">
			    <uni-tag :text="item" v-for="(item, i) in historyList" :key="i" @click="gotoGoodList(item)"></uni-tag>
			  </view>
			</view>
		</view>
	</view>	
</template>

<script>
	export default {
		data() {
			return {
				kw:'',//搜索关键词 ,
				searchList:[],//搜索结果列表
				historyList:[],//搜索关键词列表
				timer:null,//记录定时器编号
				
			};
		},
		computed:{
			// historylist(){
				
			// 	//去重并且翻转数组
			//  return Array.from(new Set([...this.historyList]))
			// }
		},
		methods:{
			input(val){
				//通过防抖限制请求频率
				clearTimeout(this.timer)
				this.timer=setTimeout(()=>{
					if(val.length===0){
						return this.searchList=[]
					}
					this.kw  = val
					
					// this.historyList.push(val)
				    // this.historyList=[...this.historyList,val]
					this.getSearchResult()
					this.sethistoryList()
					
				},500)
			},
			// 请求搜索建议列表
			async getSearchResult(){
				const {data} =  await uni.$http.get('/goods/qsearch',{query:this.kw})
				this.searchList = data.message
			},
			// 点击搜索建议跳转到商品详情
			gotoDetail(id){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+id
				})
			},//保存数浏览记录
			sethistoryList(){
			 this.historyList.unshift(this.kw)
			 const set = new Set(this.historyList)
			 this.historyList=Array.from(set)
			 uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},//清空搜索记录
			clearlist(){
				this.historyList=[]
				uni.setStorageSync('kw','')
			},
			// 点击搜索历史 跳转到商品列表页
			gotoGoodList(item){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+item
				})
			}
			
			
		},
		onload(){
			this.historyList = JSON.parse(uni.getStorageSync('kw')||'[]')
		}
	}
</script>

<style lang="scss">
.uni-searchbar {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  position: relative;
  padding: 16rpx;
  /* 将默认的 #FFFFFF 改为 #C00000 */
  background-color: #c00000;
}
.sugg-list {
  padding: 0 5px;

  .sugg-item {
    font-size: 12px;
    padding: 13px 0;
    border-bottom: 1px solid #efefef;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .goods-name {
      // 文字不允许换行（单行文本）
      white-space: nowrap;
      // 溢出部分隐藏
      overflow: hidden;
      // 文本溢出后，使用 ... 代替
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}
.history-box {
  padding: 0 5px;

  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }

  .history-list {
    display: flex;
    flex-wrap: wrap;

    .uni-tag {
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}
</style>
